<template>
  <div>
    <!-- 模糊查询 -->
    <el-row>
        <el-col :span="3"
        ><el-input
          v-model="name"
          size="mini"
          placeholder="请输品牌名称"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-input
          v-model="firstLetter"
          size="mini"
          placeholder="首字母"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="search()"
          icon="el-icon-search"
          >搜索</el-button
        ></el-col>
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="addBtn()"
          icon="el-icon-circle-plus"
          >添加</el-button
        ></el-col
      >
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="flush()"
          icon="el-icon-search"
          >刷新</el-button
        ></el-col
      >
    </el-row>
    <!-- 功能列表 -->
    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="车辆名称"> </el-table-column>
        <el-table-column prop="firstLetter" label="首字母"> </el-table-column>
        <el-table-column prop="sort" label="类型"> </el-table-column>
         <el-table-column prop="factoryStatus" :formatter="getfactoryStatus" label="品牌制造商"> </el-table-column>
        <el-table-column prop="showStatus" :formatter="getShowStatus" label="车辆状态"> </el-table-column>
        <el-table-column prop="logo" label="车标">
            <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.avatar"
            ></el-image>
        </template>
             </el-table-column>
         <el-table-column prop="bigPic" label="车辆图片"> 
             <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.avatar"
            ></el-image>
          </template>
         </el-table-column>
        <el-table-column prop="brandStory" label="品牌故事"> </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button size="mini" @click="toEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页 -->
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <el-row>
      <!--添加弹出框-->
      <el-dialog title="添加" :visible.sync="addFormVisible">
        <el-form :model="form">
          <el-form-item label="车辆名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="首字母" :label-width="formLabelWidth">
            <el-input v-model="form.firstLetter" autocomplete="off"></el-input>
          </el-form-item>
           <el-form-item label="类型" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
           <el-form-item label="品牌制造商" :label-width="formLabelWidth">
           <el-radio v-model="form.factoryStatus" label="0">是</el-radio>
            <el-radio v-model="form.factoryStatus" label="1">否</el-radio>
          </el-form-item>
           <el-form-item label="车辆状态" :label-width="formLabelWidth">
           <el-radio v-model="form.showStatus" label="0">有</el-radio>
            <el-radio v-model="form.showStatus" label="1">无</el-radio>
          </el-form-item>
           <el-form-item label="车标" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="/commonOSS/upload"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
           <el-form-item label="图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="/commonOSS/upload"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
           <el-form-item label="品牌故事" :label-width="formLabelWidth">
            <el-input v-model="form.brandStory" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add()">确 定</el-button>
        </div>
      </el-dialog>
    </el-row>
    <el-row>
      <!--更新弹出框-->
      <el-dialog title="更新" :visible.sync="updateFormVisible">
        <el-form :model="form">
          <el-form-item label="车辆名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="首字母" :label-width="formLabelWidth">
            <el-input v-model="form.firstLetter" autocomplete="off"></el-input>
          </el-form-item>
           <el-form-item label="类型" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
           <el-form-item label="品牌制造商" :label-width="formLabelWidth">
            <el-radio v-model="form.factoryStatus" :label="0">是</el-radio>
            <el-radio v-model="form.factoryStatus" :label="1">否</el-radio>
          </el-form-item>
           <el-form-item label="车辆状态" :label-width="formLabelWidth">
           <el-radio v-model="form.showStatus" :label="0">有</el-radio>
            <el-radio v-model="form.showStatus" :label="1">无</el-radio>
          </el-form-item>
           <el-form-item label="车标" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="/commonOSS/upload"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
           <el-form-item label="图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="/commonOSS/upload"
              name="file"
              :auto-upload="true"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
           <el-form-item label="品牌故事" :label-width="formLabelWidth">
            <el-input v-model="form.brandStory" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="updateFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update()">确 定</el-button>
        </div>
      </el-dialog>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //数据
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 20,
      addFormVisible: false, //添加对话框是否显示
      updateFormVisible: false, //更新对话框是否显示
      formLabelWidth: "120px",
      name: "",
      firstLetter: "",
      id: "",

      form:{}
    };
  },
  created: function () {
    this.loadData();
  },
  methods: {
    //方法
    //加载数据的方法
    loadData: function () {
      var _this = this;
      //初始化数据
      this.$http
        .get("http://localhost:9999/cars/brand/queryAll", {
          params: {
            pageNo: this.currentPage,
            pageSize: this.pageSize,
            name: this.name,
            firstLetter: this.firstLetter,
          }
        })
        .then(function (response) {
          // handle success  处理成功，回调的方法
          //console.log(response);
          //赋值分页列表
          _this.tableData = response.data.data.list;
          //删除最后一页为空时，调到上一页
          //alert(_this.tableData.length);
          if (_this.tableData.length == 0 && _this.currentPage != 1) {
            _this.currentPage = _this.currentPage - 1;
            _this.loadData();
          }
          //赋值总条数
          _this.total = response.data.data.total;
        })
        .catch(function (error) {
          // handle error 处理异常
          //console.log(error);
        })
        .then(function () {
          // always executed  总是执行  相当于finally
        });
    },
    //每页数量发生变化时
    handleSizeChange: function (v) {
      this.pageSize = v;
      this.loadData();
    },
    //当前页数放生变化是
    handleCurrentChange: function (v) {
      this.currentPage = v;
      this.loadData();
    },
    // 搜索方法
    search: function () {
      this.currentPage = 1;
      this.loadData();
    },
    //跳转更新
    toEdit: function (index, row) {
      //清空原来的
      this.form.avatar = "";
      this.form = row;
      this.updateFormVisible = true;
    },
    addBtn:function(){
        this.form={},
        this.addFormVisible=true
    },
   
    //删除
    handleDelete: function (index, row) {
      var _this = this;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .delete(
              "http://localhost:9999/cars/brand/deleteBrand?id=" + row.id
            )
            .then(function (response) {
              console.log(response);
              if (response.status == 200) {
                if (response.data.data) {
                  //提示成功
                  _this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                  //重新加载数据
                  _this.loadData();
                }
              }
            })
            .catch(function (error) {
              console.log(error);
              _this.$message.error("你错了。。。");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //添加
    add: function () {
      var _this = this;
      this.$http
        .post("http://localhost:9999/cars/brand/insertBrand", this.form)
        .then(function (response) {
          console.log(response);
          if (response.status == 200) {
            //提示成功
            _this.$message({
              message: "恭喜你，添加成功",
              type: "success",
            });
            //_this.$message.success("恭喜你，添加成功");
            //关闭弹出框
            _this.addFormVisible = false;
            //重新加载数据
            _this.loadData();
            //清空form
            _this.form = {};
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
   
    getShowStatus(row, column, cellValue) {
       return cellValue == 0 ? "有" : "无";
    },
     getfactoryStatus(row, column, cellValue) {
      return cellValue == 0 ? "是" : "否";
    },
    //更新
    update: function () {
      var _this = this;
      //JSON.stringify JSON 转 String
      // alert(JSON.stringify(this.form));
      this.$http
        .put("http://localhost:9999/cars/brand/updateBrand", this.form)
        .then(function (response) {
          console.log(response);
          if (response.status == 200) {
            //提示成功
            _this.$message({
              message: "恭喜你，操作成功",
              type: "success",
            });
            //_this.$message.success("恭喜你，添加成功");
            //关闭弹出框
            _this.updateFormVisible = false;
            //重新加载数据
            _this.loadData();
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    flush() {
      //刷新当前页
      location.reload();
    }
  } 
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
